<?php
namespace clt;
class Curl{
	/*get请求*/
    public static function _get($url,$header=[]){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        //file_put_contents("cur111.log", json_encode($aStatus)."\n",FILE_APPEND);
        //file_put_contents("cur222.log", $sContent."\n",FILE_APPEND);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            $aStatus['time'] = date('Y-m-d H:i:s',time());
            $result = json_decode($sContent,true);
            if(is_array($result)){
                return json_encode($result);
            }  
            return false;
        }
    }
	/*post请求*/
    public static function _post($url,$param=array(),$post_file=false,$header=[],$timeout=15){
        $oCurl = curl_init();
        if(stripos($url,"https://") !== FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST =  join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
        //curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);

        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            $result = json_decode($sContent,true);
            if(is_array($result)){
                return json_encode($result);
            }   
            return false;
        }
    }
}